﻿using System;
using System.Collections;
using System.Collections.Generic;
using PEProtocol;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

/**
 * 控制界面
 */

public class PlayerCtrlWnd : WindowRoot {
    public Image imgTouch;
    public Image imgDirBg;
    public Image imgDirPoint;
    public Transform expPrgTrans;
    public Text txtLevel;
    public Text txtName;
    public Text txtExpPrg;

    private Vector2 startPos = Vector2.zero;
    private Vector2 defaultPos = Vector2.zero;
    private float pointDis;

    public Vector2 currentDir = Vector2.zero;
    
    protected override void InitWnd(){
        base.InitWnd();
        defaultPos = imgDirBg.transform.position;
        pointDis = Screen.height * 1.0f / Constants.ScreenStandardHeight * Constants.ScreenOpDis;
        SetActive(imgDirPoint, false);
        RegisterTouchEvents();
        skill1CDTime = resSvc.GetSkillCfg(101).cdTime / 1000.0f;
        skill2CDTime = resSvc.GetSkillCfg(102).cdTime / 1000.0f;
        skill3CDTime = resSvc.GetSkillCfg(103).cdTime / 1000.0f;
        RefreshUI();
    }
    
    public void RefreshUI(){
        PlayerData pd = GameRoot.Instance.PlayerData;
        
        SetText(txtLevel, pd.level);
        SetText(txtName, pd.name);

        //expPrg
        int expPrgVal = (int) (pd.exp * 1.0f / PECommon.GetExpUpValByLevel(pd.level) * 100);
        SetText(txtExpPrg, expPrgVal + "%");

        int index = expPrgVal / 10;

        GridLayoutGroup grid = expPrgTrans.GetComponent<GridLayoutGroup>();

        float globalRate = 1.0f * Constants.ScreenStandardHeight / Screen.height;
        float ScreenWidth = Screen.width * globalRate;
        float width = (ScreenWidth - 180) / 10;

        grid.cellSize = new Vector2(width, 7);

        for (int i = 0; i < expPrgTrans.childCount; i++){
            Image img = expPrgTrans.GetChild(i).GetComponent<Image>();
            if (i < index){
                img.fillAmount = 1;
            }
            else if (i == index){
                img.fillAmount = expPrgVal % 10 * 1.0f / 10;
            }
            else{
                img.fillAmount = 0;
            }
        }
    }
    
    public void RegisterTouchEvents(){
        OnClickDown(imgTouch.gameObject, (PointerEventData data) => {
            imgDirBg.transform.position = data.position;
            startPos = data.position;
            SetActive(imgDirPoint);
            imgDirPoint.transform.position = data.position;
        });

        OnClickUp(imgTouch.gameObject, (PointerEventData data) => {
            imgDirBg.transform.position = defaultPos;
            SetActive(imgDirPoint, false);
            imgDirPoint.transform.localPosition = Vector2.zero;
            currentDir = Vector2.zero;
            //TODO 方向信息传递
            BattleSys.Instance.SetSelfPlayerMoveDir(Vector2.zero);
        });

        OnDraw(imgTouch.gameObject, (PointerEventData data) => {
            Vector2 dir = data.position - startPos;
            float len = dir.magnitude;
            if (len > pointDis){
                Vector2 clampDir = Vector2.ClampMagnitude(dir, pointDis);
                imgDirPoint.transform.position = startPos + clampDir;
            }
            else{
                imgDirPoint.transform.position = data.position;
            }

            currentDir = dir.normalized;
            BattleSys.Instance.SetSelfPlayerMoveDir(currentDir);
        });
    }
    
    public void ClickNormalAtk(){
        BattleSys.Instance.ReqReleaseSkill(0);        
    }

    #region skill1
    public Image imgSkill1CD;
    public Text txtSkill1CD;

    private bool isSkill1CD = false;
    private float skill1CDTime;
    private int skill1Num;
    private float skill1FillCount = 0;
    private float skill1NumCount = 0;
    #endregion
    
    #region skill2
    public Image imgSkill2CD;
    public Text txtSkill2CD;

    private bool isSkill2CD = false;
    private float skill2CDTime;
    private int skill2Num;
    private float skill2FillCount = 0;
    private float skill2NumCount = 0;
    #endregion
    
    #region skill3
    public Image imgSkill3CD;
    public Text txtSkill3CD;

    private bool isSkill3CD = false;
    private float skill3CDTime;
    private int skill3Num;
    private float skill3FillCount = 0;
    private float skill3NumCount = 0;
    #endregion
    
    public void ClickSkill1(){
        if (isSkill1CD == false){
            BattleSys.Instance.ReqReleaseSkill(1);
            isSkill1CD = true;
            SetActive(imgSkill1CD);
            imgSkill1CD.fillAmount = 1;
            skill1Num = (int) skill1CDTime;
            SetText(txtSkill1CD, skill1Num);
        }
    }
    
    public void ClickSkill2(){
        if (isSkill2CD == false){
            BattleSys.Instance.ReqReleaseSkill(2);
            isSkill2CD = true;
            SetActive(imgSkill2CD);
            imgSkill2CD.fillAmount = 1;
            skill2Num = (int) skill2CDTime;
            SetText(txtSkill2CD, skill2Num);
        }
    }

    public void ClickSkill3(){
        if (isSkill3CD == false){
            BattleSys.Instance.ReqReleaseSkill(3);
            isSkill3CD = true;
            SetActive(imgSkill3CD);
            imgSkill3CD.fillAmount = 1;
            skill3Num = (int) skill3CDTime;
            SetText(txtSkill3CD, skill3Num);
        }
    }
    
    //Test Reset Data
    public void ClickResetCfgs(){
        resSvc.ResetSkillCfgs();
    }

    private void Update(){
        //测试函数
        if (Input.GetKeyDown(KeyCode.K)){
            ClickSkill1();
        }

        float delta = Time.deltaTime;
        if (isSkill1CD){
            skill1FillCount += delta;
            if (skill1FillCount >= skill1CDTime){
                isSkill1CD = false;
                SetActive(imgSkill1CD, false);
                skill1FillCount = 0;
            }
            else{
                imgSkill1CD.fillAmount = 1 - (skill1FillCount / skill1CDTime);
            }

            skill1NumCount += delta;
            if (skill1NumCount >= 1){
                skill1NumCount -= 1;
                skill1Num -= 1;
                SetText(txtSkill1CD, skill1Num);
            }
        }
        
        if (isSkill2CD){
            skill2FillCount += delta;
            if (skill2FillCount >= skill2CDTime){
                isSkill2CD = false;
                SetActive(imgSkill2CD, false);
                skill2FillCount = 0;
            }
            else{
                imgSkill2CD.fillAmount = 1 - (skill2FillCount / skill2CDTime);
            }

            skill2NumCount += delta;
            if (skill2NumCount >= 1){
                skill2NumCount -= 1;
                skill2Num -= 1;
                SetText(txtSkill2CD, skill2Num);
            }
        }
        
        if (isSkill3CD){
            skill3FillCount += delta;
            if (skill3FillCount >= skill3CDTime){
                isSkill3CD = false;
                SetActive(imgSkill3CD, false);
                skill3FillCount = 0;
            }
            else{
                imgSkill3CD.fillAmount = 1 - (skill3FillCount / skill3CDTime);
            }

            skill3NumCount += delta;
            if (skill3NumCount >= 1){
                skill3NumCount -= 1;
                skill3Num -= 1;
                SetText(txtSkill3CD, skill3Num);
            }
        }
    }
}
